package com.xnj.testweb.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xnj.testweb.admin.mapper.SysUserMapper;
import com.xnj.testweb.admin.service.SysUserService;
import com.xnj.testweb.common.constants.Constants;
import com.xnj.testweb.common.exeception.CommonException;
import com.xnj.testweb.common.utils.AESUtils;
import com.xnj.testweb.common.utils.StringUtils;
import com.xnj.testweb.core.entry.SysUser;
import com.xnj.testweb.core.param.AddUserParam;
import com.xnj.testweb.core.param.LogInParam;
import com.xnj.testweb.core.param.PageUserParam;
import com.xnj.testweb.core.param.UpdatePasswordParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yujie
 * @data 2025/4/14 13:59
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public SysUser userLogin(LogInParam param) throws Exception {
        // 用户名 和 密码 不可为空
        if (StringUtils.isNull(param.getUserName()) || StringUtils.isEmpty(param.getUserName())) {
            throw new CommonException(Constants.ERROR, "用户名不可为空");
        }

        if (StringUtils.isNull(param.getPassword()) || StringUtils.isEmpty(param.getPassword())) {
            throw new CommonException(Constants.ERROR, "密码不可为空");
        }


        // 注册 需要解密
        // 使用用户名查询 用户信息
        SysUser user = sysUserMapper.selectUserByName(param.getUserName());

        if (user == null) {
            throw new CommonException(Constants.ERROR, "查询不到用户信息，请核实");
        }

        // 密码解密
        String decrypt = AESUtils.decrypt(user.getPassword());

        if (!param.getPassword().equals(decrypt)) {
            throw new CommonException(Constants.ERROR, "密码错误");
        }

        // 返回用户信息
        return user;
    }

    @Override
    public void userRegister(AddUserParam param) throws Exception {
        // 用户名 和 密码 不可为空
        if (StringUtils.isNull(param.getUserName()) || StringUtils.isEmpty(param.getUserName())) {
            throw new CommonException(Constants.ERROR, "用户名不可为空");
        }

        // 角色不可为空
        if (StringUtils.isNull(param.getUserRole()) || StringUtils.isEmpty(param.getUserRole())) {
            throw new CommonException(Constants.ERROR, "角色信息不可为空");
        }

        // 真实姓名 不可为空
        if (StringUtils.isNull(param.getRealName()) || StringUtils.isEmpty(param.getRealName())) {
            throw new CommonException(Constants.ERROR, "真实姓名不可为空");
        }


        // 查询该 用户是否 已存在
        SysUser userByName = sysUserMapper.selectUserByName(param.getUserName());
        if (StringUtils.isNotNull(userByName)) {
            throw new CommonException(Constants.ERROR, "用户名已存在");
        }

        // 密码进行加密
//        String encrypt = AESUtils.encrypt(param.getPassword());
        String encrypt = AESUtils.encrypt(Constants.DEFAULTPASSWORD);
        SysUser user = new SysUser();
        // 用户名
        user.setUserName(param.getUserName());
        // 默认密码
        user.setPassword(encrypt);
        // 角色
        user.setUserRole(param.getUserRole());
        // 真实姓名
        user.setRealName(param.getRealName());
        // 手机号
        user.setPhoneNumber(param.getPhoneNumber());
        // 邮箱
        user.setUserMail(param.getUserMail());
        // 部门
        user.setDepartment(param.getDepartment());

        sysUserMapper.insert(user);
    }

    @Override
    public SysUser selectUserByName(String userName) {
        return sysUserMapper.selectUserByName(userName);
    }

    @Override
    public SysUser queryById(Integer id) {
        return sysUserMapper.selectById(id);
    }

    @Override
    public SysUser insert(SysUser sysUser) {
        sysUserMapper.insert(sysUser);
        return sysUser;
    }

    @Override
    public SysUser update(SysUser sysUser) {
        sysUserMapper.updateById(sysUser);
        return queryById(sysUser.getId());
    }

    @Override
    public void resetPassword(String userName) throws Exception {
        // 根据 用户名查询用户信息
        if (StringUtils.isNull(userName) || StringUtils.isEmpty(userName)) {
            throw new CommonException(Constants.ERROR, "用户名不可为空");
        }

        // 查询该 用户是否 已存在
        SysUser userByName = sysUserMapper.selectUserByName(userName);
        if (StringUtils.isNull(userByName)) {
            throw new CommonException(Constants.ERROR, "用户不存在");
        }

        // 密码进行加密
        String encrypt = AESUtils.encrypt(Constants.DEFAULTPASSWORD);
        userByName.setPassword(encrypt);
        sysUserMapper.updateById(userByName);
    }

    @Override
    public void updatePassword(UpdatePasswordParam param) throws Exception {
        // 用户名
        if (StringUtils.isNull(param.getUserName()) || StringUtils.isEmpty(param.getUserName())) {
            throw new CommonException(Constants.ERROR, "用户名不可为空");
        }

        // 旧密码
        if (StringUtils.isNull(param.getOldPassword()) || StringUtils.isEmpty(param.getOldPassword())) {
            throw new CommonException(Constants.ERROR, "旧密码不可为空");
        }

        // 新密码
        if (StringUtils.isNull(param.getNewPassword()) || StringUtils.isEmpty(param.getNewPassword())) {
            throw new CommonException(Constants.ERROR, "新密码不可为空");
        }

        if (param.getOldPassword().equals(param.getNewPassword())) {
            throw new CommonException(Constants.ERROR, "新密码不可与旧密码相同");
        }

        // 根据 用户名 查询 用户信息
        SysUser userByName = sysUserMapper.selectUserByName(param.getUserName());
        if (StringUtils.isNull(userByName)) {
            throw new CommonException(Constants.ERROR, "用户不存在");
        }

        // 验证 旧密码是有 与原本密码一致
        // 密码解密
        String decrypt = AESUtils.decrypt(userByName.getPassword());
        if (!decrypt.equals(param.getOldPassword())) {
            throw new CommonException(Constants.ERROR, "旧密码错误,忘记密码请重置");
        }

        // 对新密码进行加密
        String newWord = AESUtils.encrypt(param.getNewPassword());
        userByName.setPassword(newWord);
        sysUserMapper.updateById(userByName);

        // 密码修改完成 退出 登录
        StpUtil.logout(userByName.getId());
    }

    @Override
    public SysUser selectById(Integer id) {
        return sysUserMapper.selectById(id);
    }

    @Override
    public void deleteUserById(Integer id) {
        // 判断当前登录用户角色 是否是admin, 不是不可删除
        // 根据 id 查询用户信息
        SysUser sysUser = sysUserMapper.selectById(id);
        if (sysUser == null) {
            throw new CommonException(Constants.ERROR, "系统中无该用户信息");
        }

        if (sysUser.getUserType().equals("root")) {
            throw new CommonException(Constants.ERROR, "系统内置用户不可被删除");
        }

        // 删除角色
        sysUserMapper.deleteById(id);
//        sysUserMapper.deleteUserById(id);
    }

    @Override
    public Page<SysUser> pageSysUserList(PageUserParam param) {
        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        // 分页查询
        Page<SysUser> page = new Page<>(param.getPageNum(), param.getPageSize());
        List<SysUser> list = sysUserMapper.pageSysUserList(param);

        // 页码
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());

        // 总记录数
        Long aLong = sysUserMapper.pageCount(param);
        page.setTotal(aLong);
        page.setRecords(list);

        return page;
    }
}
